package com.xbongbong.pro.service.toolbox.elasticsearch.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.SizeConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.SheetTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.model.PaasFormPaasSubDataModel;
import com.xbongbong.paas.pojo.dto.PaasFormDataEsMappingDTO;
import com.xbongbong.paas.pojo.dto.PhysicalDeleteDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.domain.entity.ReRollBackEntity;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.AssembleSubDataEntity;
import com.xbongbong.saas.domain.entity.BomBillSubDataEntity;
import com.xbongbong.saas.domain.entity.CommunicatePlanSubDataEntity;
import com.xbongbong.saas.domain.entity.ContactSubDataEntity;
import com.xbongbong.saas.domain.entity.ContractPerformanceEntity;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.domain.entity.ContractSubDataEntity;
import com.xbongbong.saas.domain.entity.ContractUserEntity;
import com.xbongbong.saas.domain.entity.CustomerCommunicateSubDataEntity;
import com.xbongbong.saas.domain.entity.CustomerSubDataEntity;
import com.xbongbong.saas.domain.entity.FundTransferSubDataEntity;
import com.xbongbong.saas.domain.entity.InstockSubDataEntity;
import com.xbongbong.saas.domain.entity.InventorySubDataEntity;
import com.xbongbong.saas.domain.entity.InvoiceSubDataEntity;
import com.xbongbong.saas.domain.entity.OpportunitySubDataEntity;
import com.xbongbong.saas.domain.entity.OtherExpenseSubDataEntity;
import com.xbongbong.saas.domain.entity.OtherIncomeSubDataEntity;
import com.xbongbong.saas.domain.entity.OutstockSubDataEntity;
import com.xbongbong.saas.domain.entity.PayPlanSheetSubDataEntity;
import com.xbongbong.saas.domain.entity.PayPlanSheetUserEntity;
import com.xbongbong.saas.domain.entity.PayPlanSubDataEntity;
import com.xbongbong.saas.domain.entity.PayPlanUserEntity;
import com.xbongbong.saas.domain.entity.PaymentSheetSubDataEntity;
import com.xbongbong.saas.domain.entity.PaymentSheetUserEntity;
import com.xbongbong.saas.domain.entity.PaymentSubDataEntity;
import com.xbongbong.saas.domain.entity.PaymentTaskSubDataEntity;
import com.xbongbong.saas.domain.entity.PaymentTaskUserEntity;
import com.xbongbong.saas.domain.entity.PaymentUserEntity;
import com.xbongbong.saas.domain.entity.ProductSubDataEntity;
import com.xbongbong.saas.domain.entity.ProductionOrderSubDataEntity;
import com.xbongbong.saas.domain.entity.PurchaseInvoiceSubDataEntity;
import com.xbongbong.saas.domain.entity.PurchaseSubDataEntity;
import com.xbongbong.saas.domain.entity.RefundSubDataEntity;
import com.xbongbong.saas.domain.entity.RefundUserEntity;
import com.xbongbong.saas.domain.entity.ReturnedPurchaseSubDataEntity;
import com.xbongbong.saas.domain.entity.SupplierCommunicateSubDataEntity;
import com.xbongbong.saas.domain.entity.SupplierContactSubDataEntity;
import com.xbongbong.saas.domain.entity.SupplierSubDataEntity;
import com.xbongbong.saas.domain.entity.TransferSubDataEntity;
import com.xbongbong.saas.domain.entity.WarehouseSubDataEntity;
import com.xbongbong.saas.domain.entity.WorkOrderCcEntity;
import com.xbongbong.saas.domain.entity.WorkOrderFlowUserEntity;
import com.xbongbong.saas.domain.entity.WorkOrderSubDataEntity;
import com.xbongbong.saas.domain.entity.WorkReportSubDataEntity;
import com.xbongbong.saas.domain.entity.ext.ContractEntityExt;
import com.xbongbong.saas.domain.entity.ext.PayPlanEntityExt;
import com.xbongbong.saas.domain.entity.ext.PayPlanSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PurchaseEntityExt;
import com.xbongbong.saas.enums.PaymentLinkTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.subform.ContactTeamEnum;
import com.xbongbong.saas.enums.subform.ContractPerformanceSubFormEnum;
import com.xbongbong.saas.enums.subform.ContractProductSubFormEnum;
import com.xbongbong.saas.enums.subform.PayPlanSheetTeamEnum;
import com.xbongbong.saas.enums.subform.PayPlanTeamEnum;
import com.xbongbong.saas.enums.subform.PaymentSheetTeamEnum;
import com.xbongbong.saas.enums.subform.PaymentTaskTeamEnum;
import com.xbongbong.saas.enums.subform.PaymentTeamEnum;
import com.xbongbong.saas.enums.subform.RefundTeamEnum;
import com.xbongbong.saas.enums.subform.WorkOrderCcTeamEnum;
import com.xbongbong.saas.enums.subform.WorkOrderTeamEnum;
import com.xbongbong.saas.model.AssembleSubDataModel;
import com.xbongbong.saas.model.BomBillSubDataModel;
import com.xbongbong.saas.model.CommunicatePlanSubDataModel;
import com.xbongbong.saas.model.ContactSubDataModel;
import com.xbongbong.saas.model.ContractModel;
import com.xbongbong.saas.model.ContractSubDataModel;
import com.xbongbong.saas.model.CustomerCommunicateSubDataModel;
import com.xbongbong.saas.model.CustomerSubDataModel;
import com.xbongbong.saas.model.FundTransferSubDataModel;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.saas.model.InstockSubDataModel;
import com.xbongbong.saas.model.InventorySubDataModel;
import com.xbongbong.saas.model.InvoiceSubDataModel;
import com.xbongbong.saas.model.OpportunitySubDataModel;
import com.xbongbong.saas.model.OtherExpenseSubDataModel;
import com.xbongbong.saas.model.OtherIncomeSubDataModel;
import com.xbongbong.saas.model.OutstockSubDataModel;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import com.xbongbong.saas.model.PayPlanModel;
import com.xbongbong.saas.model.PayPlanSheetModel;
import com.xbongbong.saas.model.PayPlanSheetSubDataModel;
import com.xbongbong.saas.model.PayPlanSheetUserModel;
import com.xbongbong.saas.model.PayPlanSubDataModel;
import com.xbongbong.saas.model.PayPlanUserModel;
import com.xbongbong.saas.model.PaymentModel;
import com.xbongbong.saas.model.PaymentSheetModel;
import com.xbongbong.saas.model.PaymentSheetSubDataModel;
import com.xbongbong.saas.model.PaymentSheetUserModel;
import com.xbongbong.saas.model.PaymentSubDataModel;
import com.xbongbong.saas.model.PaymentTaskSubDataModel;
import com.xbongbong.saas.model.PaymentTaskUserModel;
import com.xbongbong.saas.model.PaymentUserModel;
import com.xbongbong.saas.model.ProductSubDataModel;
import com.xbongbong.saas.model.ProductionOrderSubDataModel;
import com.xbongbong.saas.model.PurchaseInvoiceSubDataModel;
import com.xbongbong.saas.model.PurchaseModel;
import com.xbongbong.saas.model.PurchaseSubDataModel;
import com.xbongbong.saas.model.QuotationSubDataModel;
import com.xbongbong.saas.model.RefundInvoiceModel;
import com.xbongbong.saas.model.RefundSubDataModel;
import com.xbongbong.saas.model.RefundUserModel;
import com.xbongbong.saas.model.ReturnedPurchaseSubDataModel;
import com.xbongbong.saas.model.SupplierCommunicateSubDataModel;
import com.xbongbong.saas.model.SupplierContactSubDataModel;
import com.xbongbong.saas.model.SupplierSubDataModel;
import com.xbongbong.saas.model.TransferSubDataModel;
import com.xbongbong.saas.model.WarehouseSubDataModel;
import com.xbongbong.saas.model.WorkOrderFlowUserModel;
import com.xbongbong.saas.model.WorkOrderSubDataModel;
import com.xbongbong.saas.model.WorkReportSubDataModel;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * 这个helper专门用于发生异常时,数据库回退数据,es删除以及es数据还原
 *
 * @author 魏荣杰
 * @date 2019/5/23 20:44
 * @since v1.0
 * @version v1.0
 */
@Component
public class ProEsHelper {
    private static final Logger LOG = LoggerFactory.getLogger(ProEsHelper.class);

    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private WorkOrderFlowUserModel workOrderFlowUserModel;
    @Resource
    private EsHelper esHelper;
    @Resource
    private ContractModel contractModel;
    @Resource
    private RefundInvoiceModel refundInvoiceModel;
    @Resource
    private RefundUserModel refundUserModel;
    @Resource
    private PaymentModel paymentModel;
    @Resource
    private PaymentSheetModel paymentSheetModel;
    @Resource
    private PurchaseModel purchaseModel;
    @Resource
    private PayPlanModel payPlanModel;
    @Resource
    private PayPlanSheetModel payPlanSheetModel;
    @Resource
    private PaymentTaskUserModel paymentTaskUserModel;
    @Resource
    private PaymentUserModel paymentUserModel;
    @Resource
    private PaymentSheetUserModel paymentSheetUserModel;
    @Resource
    private PayPlanUserModel payPlanUserModel;
    @Resource
    private PayPlanSheetUserModel payPlanSheetUserModel;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;
    @Resource
    private PaasFormPaasSubDataModel paasFormPaasSubDataModel;
    @Resource
    private CustomerSubDataModel customerSubDataModel;
    @Resource
    private ContractSubDataModel contractSubDataModel;
    @Resource
    private OutstockSubDataModel outstockSubDataModel;
    @Resource
    private ContactSubDataModel contactSubDataModel;
    @Resource
    private InvoiceSubDataModel invoiceSubDataModel;
    @Resource
    private PurchaseInvoiceSubDataModel purchaseInvoiceSubDataModel;
    @Resource
    private InstockSubDataModel instockSubDataModel;
    @Resource
    private AssembleSubDataModel assembleSubDataModel;
    @Resource
    private PaymentSubDataModel paymentSubDataModel;
    @Resource
    private PaymentSheetSubDataModel paymentSheetSubDataModel;
    @Resource
    private PaymentTaskSubDataModel paymentTaskSubDataModel;
    @Resource
    private PayPlanSheetSubDataModel payPlanSheetSubDataModel;
    @Resource
    private ProductionOrderSubDataModel productionOrderSubDataModel;
    @Resource
    private PayPlanSubDataModel payPlanSubDataModel;
    @Resource
    private CommunicatePlanSubDataModel communicatePlanSubDataModel;
    @Resource
    private OpportunitySubDataModel opportunitySubDataModel;
    @Resource
    private BomBillSubDataModel bomBillSubDataModel;
    @Resource
    private CustomerCommunicateSubDataModel customerCommunicateSubDataModel;
    @Resource
    private SupplierCommunicateSubDataModel supplierCommunicateSubDataModel;
    @Resource
    private WorkReportSubDataModel workReportSubDataModel;
    @Resource
    private PurchaseSubDataModel purchaseSubDataModel;
    @Resource
    private ReturnedPurchaseSubDataModel returnedPurchaseSubDataModel;
    @Resource
    private SupplierContactSubDataModel supplierContactSubDataModel;
    @Resource
    private SupplierSubDataModel supplierSubDataModel;
    @Resource
    private ProductSubDataModel productSubDataModel;
    @Resource
    private InventorySubDataModel inventorySubDataModel;
    @Resource
    private TransferSubDataModel transferSubDataModel;
    @Resource
    private WarehouseSubDataModel warehouseSubDataModel;
    @Resource
    private RefundSubDataModel refundSubDataModel;
    @Resource
    private WorkOrderSubDataModel workOrderSubDataModel;
    @Resource
    private QuotationSubDataModel quotationSubDataModel;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;
    @Resource
    private OtherIncomeSubDataModel otherIncomeSubDataModel;
    @Resource
    private OtherExpenseSubDataModel otherExpenseSubDataModel;
    @Resource
    private FundTransferSubDataModel fundTransferSubDataModel;
    @Resource
    private IndexTypeModel indexTypeModel;


    /**
     * 用于新建的回滚（单个新建使用，批量不适用）
     * Description: 物理删除所有es数据,删除主体数据以及相关的子表单,以及相关联的业务es数据(新建时异常回退使用)
     * @param id 主体id
     * @param corpid 公司id
     * @param indexTypeEnum es索引
     * @author 魏荣杰
     * @date 2019/5/23 9:31
     * @since v1.0
     */
    public void physicalDeleteAllInAdd(Long id, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        try {
            //id为空没必要往下进行
            if (id == null || id == 0) {
                return ;
            }
            //删除主表es数据
            PhysicalDeleteDTO physicalDeleteDTO = new PhysicalDeleteDTO();
            physicalDeleteDTO.setEsId(corpid + "_" + id);
            physicalDeleteDTO.setIndex(indexTypeEnum.getIndex());
            physicalDeleteDTO.setType(indexTypeEnum.getType());
            paasEsModel.physicalDelete(physicalDeleteDTO);
            //删除附属表数据（如果有）
            switch (indexTypeEnum) {
                case IDX_FORM_DATA:
                    // 子表单的删除
                    List<PaasFormSubDataEntity> paasFormSubDataEntities = paasFormPaasSubDataModel.getByDataId(id, corpid);
                    if (!paasFormSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, paasFormSubDataEntities, IndexTypeEnum.IDX_SUB_FORM_DATA);
                    }
                    break;
                case IDX_SAAS_CUSTOMER:
                    // 子表单的删除
                    List<CustomerSubDataEntity> customerSubDataEntities = customerSubDataModel.getByDataId(id, corpid);
                    if (!customerSubDataEntities.isEmpty()) {
                        IndexTypeEnum customerSubIndex = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());
                        physicalDeleteSubBatch(corpid, customerSubDataEntities, customerSubIndex);
                    }
                    break;
                case IDX_SAAS_CUSTOMER_TEAM:
                    break;
                case IDX_SAAS_CONTRACT:
                    // 子表单的删除
                    List<ContractSubDataEntity> contractSubDataEntities = contractSubDataModel.getByDataId(id, corpid);
                    if (!contractSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, contractSubDataEntities, IndexTypeEnum.IDX_SAAS_CONTRACT_SUB);
                    }
                    break;
                case IDX_SAAS_CONTRACT_PRODUCT:
                    break;
                case IDX_SAAS_CONTRACT_TEAM:
                    break;
                case IDX_SAAS_CONTRACT_PERFORMANCE:
                    break;
                case IDX_SAAS_OUTSTOCK:
                    // 子表单的删除
                    List<OutstockSubDataEntity> outstockSubDataEntities = outstockSubDataModel.getByDataId(id, corpid);
                    if (!outstockSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, outstockSubDataEntities, IndexTypeEnum.IDX_SAAS_OUTSTOCK_SUB);
                    }
                    break;
                case IDX_SAAS_CONTACT:
                    // 子表单的删除
                    List<ContactSubDataEntity> contactSubDataEntities = contactSubDataModel.getByDataId(id, corpid);
                    if (!contactSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, contactSubDataEntities, IndexTypeEnum.IDX_SAAS_CONTACT_SUB);
                    }
                    break;
                case IDX_SAAS_CONTACT_TEAM:
                    break;
                case IDX_SAAS_INVOICE:
                    // 子表单的删除
                    List<InvoiceSubDataEntity> invoiceSubDataEntities = invoiceSubDataModel.getByDataId(id, corpid);
                    if (!invoiceSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, invoiceSubDataEntities, IndexTypeEnum.IDX_SAAS_INVOICE_SUB);
                    }
                    break;
                case IDX_SAAS_PURCHASE_INVOICE:
                    // 子表单的删除
                    List<PurchaseInvoiceSubDataEntity> purchaseInvoiceSubDataEntities = purchaseInvoiceSubDataModel.getByDataId(id, corpid);
                    if (!purchaseInvoiceSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, purchaseInvoiceSubDataEntities, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_SUB);
                    }
                    break;
                case IDX_SAAS_INSTOCK:
                    // 子表单的删除
                    List<InstockSubDataEntity> instockSubDataEntities = instockSubDataModel.getByDataId(id, corpid);
                    if (!instockSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, instockSubDataEntities, IndexTypeEnum.IDX_SAAS_INSTOCK_SUB);
                    }
                    break;
                case IDX_SAAS_ASSEMBLE:
                    // 子表单的删除
                    List<AssembleSubDataEntity> assembleSubDataEntities = assembleSubDataModel.getByDataId(id, corpid);
                    if (!assembleSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, assembleSubDataEntities, IndexTypeEnum.IDX_SAAS_ASSEMBLE_SUB);
                    }
                    break;
                case IDX_SAAS_PAYMENT:
                    // 子表单的删除
                    List<PaymentSubDataEntity> paymentSubDataEntities = paymentSubDataModel.getByDataId(id, corpid);
                    if (!paymentSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, paymentSubDataEntities, IndexTypeEnum.IDX_SAAS_PAYMENT_SUB);
                    }
                    //删除回款计划团队
                    IndexTypeEnum idxPaymentTeam = IndexTypeEnum.IDX_SAAS_PAY_PLAN_TEAM;
                    List<PaasFormDataEntity> paymentUserEntities =  search(idxPaymentTeam, corpid, PaymentTeamEnum.getEsAttr(PaymentTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
                    List<PaymentUserEntity> paymentUserTeamList = (List<PaymentUserEntity>) transferSubFormHelper.transferFormDataToUserTeamList(idxPaymentTeam, paymentUserEntities);
                    List<DeleteDTO> deletePaymentUserList = new ArrayList<>();
                    for (PaymentUserEntity entity : paymentUserTeamList) {
                        DeleteDTO deleteDTO = new DeleteDTO();
                        deleteDTO.setEsId(idxPaymentTeam.getType()+ "_" + corpid +"_"+ + entity.getId());
                        deleteDTO.setIndexTypeEnum(idxPaymentTeam);
                        deleteDTO.setParent(corpid + "_" + entity.getDataId());
                        deletePaymentUserList.add(deleteDTO);
                    }
                    if (CollectionUtils.isNotEmpty(deletePaymentUserList)) {
                        paasEsModel.physicalDeleteBatchChild(deletePaymentUserList, corpid, idxPaymentTeam);
                    }
                    break;
                case IDX_SAAS_PAYMENT_TEAM:
                    break;
                case IDX_SAAS_PAYMENT_SHEET:
                    // 子表单的删除
                    List<PaymentSheetSubDataEntity> paymentSheetSubDataEntities = paymentSheetSubDataModel.getByDataId(id, corpid);
                    if (!paymentSheetSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, paymentSheetSubDataEntities, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
                    }
                    //删除回款单团队
                    IndexTypeEnum idxPaymentSheetTeam = IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_TEAM;
                    List<PaasFormDataEntity> paymentSheetUserEntities =  search(idxPaymentSheetTeam, corpid, PaymentSheetTeamEnum.getEsAttr(PaymentSheetTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
                    List<PaymentSheetUserEntity> paymentSheetUserTeamList = (List<PaymentSheetUserEntity>) transferSubFormHelper.transferFormDataToUserTeamList(idxPaymentSheetTeam, paymentSheetUserEntities);
                    List<DeleteDTO> deletePaymentSheetUserList = new ArrayList<>();
                    for (PaymentSheetUserEntity entity : paymentSheetUserTeamList) {
                        DeleteDTO deleteDTO = new DeleteDTO();
                        deleteDTO.setEsId(idxPaymentSheetTeam.getType()+ "_" + corpid +"_"+ + entity.getId());
                        deleteDTO.setIndexTypeEnum(idxPaymentSheetTeam);
                        deleteDTO.setParent(corpid + "_" + entity.getDataId());
                        deletePaymentSheetUserList.add(deleteDTO);
                    }
                    if (CollectionUtils.isNotEmpty(deletePaymentSheetUserList)) {
                        paasEsModel.physicalDeleteBatchChild(deletePaymentSheetUserList, corpid, idxPaymentSheetTeam);
                    }
                    break;
                case IDX_SAAS_PAYMENT_TASK:
                    // 子表单的删除
                    List<PaymentTaskSubDataEntity> paymentTaskSubDataEntities = paymentTaskSubDataModel.getByDataId(id, corpid);
                    if (!paymentTaskSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, paymentTaskSubDataEntities, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
                    }
                    break;
                case IDX_SAAS_PAYPLAN_SHEET:
                    // 子表单的删除
                    List<PayPlanSheetSubDataEntity> payPlanSheetSubDataEntities = payPlanSheetSubDataModel.getByDataId(id, corpid);
                    if (!payPlanSheetSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, payPlanSheetSubDataEntities, IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_SUB);
                    }
                    //删除付款单团队
                    IndexTypeEnum idxPaySheetTeam = IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_TEAM;
                    List<PaasFormDataEntity> paySheetUserEntities =  search(idxPaySheetTeam, corpid, PayPlanSheetTeamEnum.getEsAttr(PayPlanSheetTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
                    List<PayPlanSheetUserEntity> paySheetUserTeamList = (List<PayPlanSheetUserEntity>) transferSubFormHelper.transferFormDataToUserTeamList(idxPaySheetTeam, paySheetUserEntities);
                    List<DeleteDTO> deletePaySheetUserList = new ArrayList<>();
                    for (PayPlanSheetUserEntity entity : paySheetUserTeamList) {
                        DeleteDTO deleteDTO = new DeleteDTO();
                        deleteDTO.setEsId(idxPaySheetTeam.getType()+ "_" + corpid +"_"+ + entity.getId());
                        deleteDTO.setIndexTypeEnum(idxPaySheetTeam);
                        deleteDTO.setParent(corpid + "_" + entity.getDataId());
                        deletePaySheetUserList.add(deleteDTO);
                    }
                    if (CollectionUtils.isNotEmpty(deletePaySheetUserList)) {
                        paasEsModel.physicalDeleteBatchChild(deletePaySheetUserList, corpid, idxPaySheetTeam);
                    }
                    break;
                case IDX_SAAS_PRODUCTION_ORDER:
                    // 子表单的删除
                    List<ProductionOrderSubDataEntity> productionOrderSubDataEntities = productionOrderSubDataModel.getByDataId(id, corpid);
                    if (!productionOrderSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, productionOrderSubDataEntities, IndexTypeEnum.IDX_SAAS_PRODUCTION_ORDER_SUB);
                    }
                    break;
                case IDX_SAAS_PRODUCTION_ORDER_TEAM:
                    break;
                case IDX_SAAS_PAY_PLAN:
                    // 子表单的删除
                    List<PayPlanSubDataEntity> payPlanSubDataEntities = payPlanSubDataModel.getByDataId(id, corpid);
                    if (!payPlanSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, payPlanSubDataEntities, IndexTypeEnum.IDX_SAAS_PAY_PLAN_SUB);
                    }
                    //删除付款计划团队
                    IndexTypeEnum idxPayPlanTeam = IndexTypeEnum.IDX_SAAS_PAY_PLAN_TEAM;
                    List<PaasFormDataEntity> payPlanUserEntities =  search(idxPayPlanTeam, corpid, PayPlanTeamEnum.getEsAttr(PayPlanTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
                    List<PayPlanUserEntity> payPlanUserTeamList = (List<PayPlanUserEntity>) transferSubFormHelper.transferFormDataToUserTeamList(idxPayPlanTeam, payPlanUserEntities);
                    List<DeleteDTO> deletePayPlanUserList = new ArrayList<>();
                    for (PayPlanUserEntity entity : payPlanUserTeamList) {
                        DeleteDTO deleteDTO = new DeleteDTO();
                        deleteDTO.setEsId(idxPayPlanTeam.getType()+ "_" + corpid +"_"+ + entity.getId());
                        deleteDTO.setIndexTypeEnum(idxPayPlanTeam);
                        deleteDTO.setParent(corpid + "_" + entity.getDataId());
                        deletePayPlanUserList.add(deleteDTO);
                    }
                    if (CollectionUtils.isNotEmpty(deletePayPlanUserList)) {
                        paasEsModel.physicalDeleteBatchChild(deletePayPlanUserList, corpid, idxPayPlanTeam);
                    }
                    break;
                case IDX_SAAS_PAY_PLAN_TEAM:
                    break;
                case IDX_SAAS_OTHER_INCOME:
                    // 子表单的删除
                    List<OtherIncomeSubDataEntity> otherIncomeSubDataEntities = otherIncomeSubDataModel.getByDataId(id, corpid);
                    if (!otherIncomeSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, otherIncomeSubDataEntities, IndexTypeEnum.IDX_SAAS_OTHER_INCOME_SUB);
                    }
                    break;
                case IDX_SAAS_OTHER_EXPENSE:
                    // 子表单的删除
                    List<OtherExpenseSubDataEntity> otherExpenseSubDataEntities = otherExpenseSubDataModel.getByDataId(id, corpid);
                    if (!otherExpenseSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, otherExpenseSubDataEntities, IndexTypeEnum.IDX_SAAS_OTHER_EXPENSE_SUB);
                    }
                    break;
                case IDX_SAAS_FUND_TRANSFER:
                    // 子表单的删除
                    List<FundTransferSubDataEntity> fundTransferSubDataEntities = fundTransferSubDataModel.getByDataId(id, corpid);
                    if (!fundTransferSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, fundTransferSubDataEntities, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
                    }
                    break;
                case IDX_SAAS_COMMUNICATE_PLAN:
                    // 子表单的删除
                    List<CommunicatePlanSubDataEntity> communicatePlanSubDataEntities = communicatePlanSubDataModel.getByDataId(id, corpid);
                    if (!communicatePlanSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, communicatePlanSubDataEntities, IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_SUB);
                    }
                    break;
                case IDX_SAAS_OPPORTUNITY:
                    // 子表单的删除
                    List<OpportunitySubDataEntity> opportunitySubDataEntities = opportunitySubDataModel.getByDataId(id, corpid);
                    if (!opportunitySubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, opportunitySubDataEntities, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_SUB);
                    }
                    break;
                case IDX_SAAS_OPPORTUNITY_TEAM:
                    break;
                case IDX_SAAS_BOM:
                    // 子表单的删除
                    List<BomBillSubDataEntity> bomBillSubDataEntities = bomBillSubDataModel.getByDataId(id, corpid);
                    if (!bomBillSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, bomBillSubDataEntities, IndexTypeEnum.IDX_SAAS_BOM_SUB);
                    }
                    break;
                case IDX_SAAS_BOM_TEAM:
                    break;
                case IDX_SAAS_CUSTOMER_COMMUNICATE:
                    // 子表单的删除
                    List<CustomerCommunicateSubDataEntity> customerCommunicateSubDataEntities = customerCommunicateSubDataModel.getByDataId(id, corpid);
                    if (!customerCommunicateSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, customerCommunicateSubDataEntities, IndexTypeEnum.IDX_SAAS_CUSTOMER_COMMUNICATE_SUB);
                    }
                    break;
                case IDX_SAAS_SUPPLIER_COMMUNICATE:
                    // 子表单的删除
                    List<SupplierCommunicateSubDataEntity> supplierCommunicateSubDataEntities = supplierCommunicateSubDataModel.getByDataId(id, corpid);
                    if (!supplierCommunicateSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, supplierCommunicateSubDataEntities, IndexTypeEnum.IDX_SAAS_SUPPLIER_COMMUNICATE_SUB);
                    }
                    break;
                case IDX_SAAS_WORK_REPORT:
                    // 子表单的删除
                    List<WorkReportSubDataEntity> workReportSubDataEntities = workReportSubDataModel.getByDataId(id, corpid);
                    if (!workReportSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, workReportSubDataEntities, IndexTypeEnum.IDX_SAAS_WORK_REPORT_SUB);
                    }
                    break;
                case IDX_SAAS_PURCHASE:
                    // 子表单的删除
                    List<PurchaseSubDataEntity> purchaseSubDataEntities = purchaseSubDataModel.getByDataId(id, corpid);
                    if (!purchaseSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, purchaseSubDataEntities, IndexTypeEnum.IDX_SAAS_PURCHASE_SUB);
                    }
                    break;
                case IDX_SAAS_PURCHASE_TEAM:
                    break;
                case IDX_SAAS_RETURNED_PURCHASE:
                    // 子表单的删除
                    List<ReturnedPurchaseSubDataEntity> returnedPurchaseSubDataEntities = returnedPurchaseSubDataModel.getByDataId(id, corpid);
                    if (!returnedPurchaseSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, returnedPurchaseSubDataEntities, IndexTypeEnum.IDX_SAAS_RETURNED_PURCHASE_SUB);
                    }
                    break;
                case IDX_SAAS_SUPPLIER_CONTACT:
                    // 子表单的删除
                    List<SupplierContactSubDataEntity> supplierContactSubDataEntities = supplierContactSubDataModel.getByDataId(id, corpid);
                    if (!supplierContactSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, supplierContactSubDataEntities, IndexTypeEnum.IDX_SAAS_SUPPLIER_CONTACT_SUB);
                    }
                    break;
                case IDX_SAAS_SUPPLIER_CONTACT_TEAM:
                    break;
                case IDX_SAAS_SUPPLIER:
                    // 子表单的删除
                    List<SupplierSubDataEntity> supplierSubDataEntities = supplierSubDataModel.getByDataId(id, corpid);
                    if (!supplierSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, supplierSubDataEntities, IndexTypeEnum.IDX_SAAS_SUPPLIER_SUB);
                    }
                    break;
                case IDX_SAAS_SUPPLIER_TEAM:
                    break;
                case IDX_SAAS_PRODUCT:
                    // 子表单的删除
                    List<ProductSubDataEntity> productSubDataEntities = productSubDataModel.getByDataId(id, corpid);
                    if (!productSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, productSubDataEntities, IndexTypeEnum.IDX_SAAS_PRODUCT_SUB);
                    }

                    BoolQueryBuilder refundBuilder = boolQuery();
                    refundBuilder.filter(termQuery("corpid.keyword", corpid));
                    refundBuilder.filter(termQuery(ProductEnum.getAttrConnectData(ProductEnum.PARENT_ID), id));
                    refundBuilder.filter(termQuery(ParameterConstant.DEL, DelEnum.NORMAL.getDel()));
                    // 限制返回值
                    List<PaasFormDataEntityExt> childProductList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT, refundBuilder, PaasFormDataEntityExt.class, Collections.singletonList(FieldTypeEnum.DATAID.getAlias()));
                    if (CollectionsUtil.isNotEmpty(childProductList)) {
                        List<Long> childProductIdList = new ArrayList<>();
                        childProductList.forEach(result -> childProductIdList.add(result.getDataId()));
                        paasEsModel.physicalDeleteBatch(childProductIdList, corpid, IndexTypeEnum.IDX_SAAS_PRODUCT);
                    }
                    break;
                case IDX_SAAS_PRODUCT_TEAM:
                case IDX_SERVICE_PROJECT_TEAM:
                    break;
                case IDX_SAAS_INVENTORY:
                    // 子表单的删除
                    List<InventorySubDataEntity> inventorySubDataEntities = inventorySubDataModel.getByDataId(id, corpid);
                    if (!inventorySubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, inventorySubDataEntities, IndexTypeEnum.IDX_SAAS_INVENTORY_SUB);
                    }
                    break;
                case IDX_SAAS_TRANSFER:
                    // 子表单的删除
                    List<TransferSubDataEntity> transferSubDataEntities = transferSubDataModel.getByDataId(id, corpid);
                    if (!transferSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, transferSubDataEntities, IndexTypeEnum.IDX_SAAS_TRANSFER_SUB);
                    }
                    break;
                case IDX_SAAS_WAREHOUSE:
                    // 子表单的删除
                    List<WarehouseSubDataEntity> warehouseSubDataEntities = warehouseSubDataModel.getByDataId(id, corpid);
                    if (!warehouseSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, warehouseSubDataEntities, IndexTypeEnum.IDX_SAAS_WAREHOUSE_SUB);
                    }
                    break;
                case IDX_SAAS_REFUND:
                    // 子表单的删除
                    List<RefundSubDataEntity> refundSubDataEntities = refundSubDataModel.getByDataId(id, corpid);
                    if (!refundSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, refundSubDataEntities, IndexTypeEnum.IDX_SAAS_REFUND_SUB);
                    }

                    // 红冲合同Id
                    Long redContractId = null;
                    List<Long> redContractIdList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    // 原合同ID
                    Long oldContractId = 0L;
                    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
                    BoolQueryBuilder refundBuilderRefund = boolQuery();
                    refundBuilderRefund.filter(termQuery("corpid.keyword", corpid));
                    refundBuilderRefund.filter(termsQuery(FieldTypeEnum.DATAID.getAlias(), Arrays.asList(id)));
                    refundBuilderRefund.filter(termQuery("del", DelEnum.NORMAL.getDel()));
                    sourceBuilder.query(refundBuilderRefund);
                    // 限制返回值
                    List<String> fieldList = Arrays.asList(FieldTypeEnum.SERIALNO.getAlias(), RefundEnum.getAttrConnectData(RefundEnum.RED_CONTRACT_ID), RefundEnum.getAttrConnectData(RefundEnum.CONTRACT_ID), RefundEnum.getAttrConnectData(RefundEnum.PAYMENT_SHEETS), RefundEnum.getAttrConnectData(RefundEnum.INVOICES));
                    XbbAggregatedPage esEntities = esHelper.findByPage(IndexTypeEnum.IDX_SAAS_REFUND, refundBuilderRefund, PaasFormDataEntityExt.class, fieldList, 1, 1, null);
                    if (esEntities == null) {
                        return;
                    }
                    List<PaasFormDataEntityExt> resultList = esEntities.getContent();
                    if (CollectionsUtil.isNotEmpty(resultList)) {
                        JSONObject data = resultList.get(0).getData();
                        redContractId = data.getLong(RefundEnum.RED_CONTRACT_ID.getAttr());
                        oldContractId = data.getLong(RefundEnum.CONTRACT_ID.getAttr());
                        redContractIdList.add(redContractId);
                    }
                    // 物理删除新增退货退款失败的数据
                    physicalDeleteRefundInsert(id, corpid, redContractIdList, redContractId, oldContractId);
                    break;
                case IDX_SAAS_REFUND_TEAM:
                    break;
                case IDX_SAAS_WORK_ORDER:
                    // 子表单的删除
                    List<WorkOrderSubDataEntity> workOrderSubDataEntities = workOrderSubDataModel.getByDataId(id, corpid);
                    if (!workOrderSubDataEntities.isEmpty()) {
                        physicalDeleteSubBatch(corpid, workOrderSubDataEntities, IndexTypeEnum.IDX_SAAS_WORK_ORDER_SUB);
                    }

                    //删除工单团队
                    List<PaasFormDataEntity> workOrderFlowUserEntities =  search(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, corpid, WorkOrderTeamEnum.getEsAttr(WorkOrderTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
                    List<WorkOrderFlowUserEntity> userTeamEntities = (List<WorkOrderFlowUserEntity>) transferSubFormHelper.transferFormDataToUserTeamList(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, workOrderFlowUserEntities);
                    List<DeleteDTO> deleteFlowUserList = new ArrayList<>();
                    for (WorkOrderFlowUserEntity entity : userTeamEntities) {
                        DeleteDTO deleteDTO = new DeleteDTO();
                        deleteDTO.setEsId(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM + "_" + corpid + "_" + entity.getId());
                        deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
                        deleteDTO.setParent(corpid + "_" + entity.getDataId());
                        deleteFlowUserList.add(deleteDTO);
                    }
                    paasEsModel.physicalDeleteBatchChild(deleteFlowUserList, corpid, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
                    //删除工单抄送人
                    List<PaasFormDataEntity> workOrderCcEntities = search(IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC, corpid, WorkOrderCcTeamEnum.getEsAttr(WorkOrderCcTeamEnum.WORKORDER_ID), id, PaasFormDataEntity.class);
                    List<WorkOrderCcEntity> ccUserTeamEntities = (List<WorkOrderCcEntity>) transferSubFormHelper.transferFormDataToUserTeamList(IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC, workOrderCcEntities);
                    List<DeleteDTO> deleteCcList = new ArrayList<>();
                    for (WorkOrderCcEntity entity : ccUserTeamEntities) {
                        DeleteDTO deleteDTO = new DeleteDTO();
                        deleteDTO.setEsId(IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC + "_" + corpid + "_" + entity.getId());
                        deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC);
                        deleteDTO.setParent(corpid + "_" + entity.getWorkOrderId());
                        deleteCcList.add(deleteDTO);
                    }
                    paasEsModel.physicalDeleteBatchChild(deleteCcList, corpid, IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC);
                    break;
                case IDX_SAAS_WORK_ORDER_TEAM:
                    break;
                case IDX_SAAS_WORK_ORDER_CC:
                    break;
                case IDX_CALL_CENTER_CALL_LOG:
                    break;
                case UNKNOWN:
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            LOG.error("ProEsHelper.physicalDeleteAllInAdd 出错， id=" + id + "  corpid=" + corpid + "  IndexTypeEnum=" + indexTypeEnum.toString(), e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }

    }

    /**
     * Description: 物理删除子表单数据
     * @param corpid
     * @param subDataEntities
     * @param indexTypeEnum
     * @return void
     * @throws
     * @author 魏荣杰
     * @date 2020/9/30 15:35
     * @since
     */
    private void physicalDeleteSubBatch(String corpid, List<? extends PaasFormSubDataEntity> subDataEntities, IndexTypeEnum indexTypeEnum) throws XbbException {
        List<DeleteDTO> deleteSubList = new ArrayList<>();
        for (PaasFormSubDataEntity paasFormSubDataEntity : subDataEntities) {
            DeleteDTO deleteDTO = new DeleteDTO();
            deleteDTO.setEsId(corpid + "_" + paasFormSubDataEntity.getId());
            deleteDTO.setIndexTypeEnum(indexTypeEnum);
            deleteDTO.setParent(corpid + "_" + paasFormSubDataEntity.getDataId());
            deleteSubList.add(deleteDTO);
        }
        paasEsModel.physicalDeleteBatchChild(deleteSubList, corpid, indexTypeEnum);
    }

    /**
     * 用于新建的回滚（通用与不在业务端判断索引）
     * @param id
     * @param corpid
     * @param businessType
     * @param saasMark
     * @throws XbbException
     */
    public void physicalDeleteAllInAdd(Long id, String corpid, Integer businessType , Integer saasMark) throws XbbException {
        PaasFormDataEsMappingDTO paasFormDataEsMappingDTO = new PaasFormDataEsMappingDTO();
        paasFormDataEsMappingDTO.setSaasMark(saasMark);
        SheetTypeEnum sheetTypeEnum = SheetTypeEnum.getByBusinessType(businessType);
        boolean sheetFlag = SheetTypeEnum.validateSheet(sheetTypeEnum);
        if (sheetFlag) {
            paasFormDataEsMappingDTO.setBusinessType(sheetTypeEnum.getXbbRefTypeEnum().getCode());
        } else {
            paasFormDataEsMappingDTO.setBusinessType(businessType);
        }
        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid, paasFormDataEsMappingDTO.getBusinessType(), saasMark);
        physicalDeleteAllInAdd(id,corpid,indexTypeEnum);
    }

    public void otherRefEsDataRollBack(PaasFormDataEntity paasFormDataEntity, String corpid, String businessType , Integer saasMark, ReRollBackEntity reRollBackEntity) throws XbbException{
        Integer type = Integer.valueOf(businessType);
        try {
            IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid, type, saasMark);;
            switch (indexTypeEnum) {
                case IDX_SAAS_INVOICE:
                    invoiceInsertErrorAfterHandle(Collections.singletonList(paasFormDataEntity), corpid, type);
                    break;
                case IDX_SAAS_PURCHASE_INVOICE:
                    purchaseInvoiceInsertErrorAfterHandle(Collections.singletonList(paasFormDataEntity),corpid,type);
                    break;
                case IDX_SAAS_REFUND:
                case IDX_SAAS_RETURNED_PURCHASE:
                    reRedErrorHandle(corpid, type, reRollBackEntity);
                    break;
                default:
                    break;
            }
        }catch (Exception e){
            LOG.error("ProEsHelper.otherRefEsDataRollBack 出错， id=" + paasFormDataEntity.getId() + "  corpid=" + corpid + "  businessType=" + businessType, e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    /**
     * 用于更新的回滚
     * Description:
     * @param entity 业务实体,要用数据库里回滚后的数据, 如果不需要还原父表数据,可传null
     * @param id 主体id
     * @param corpid 公司id
     * @param indexTypeEnum
     * @return void
     * @throws XbbException XbbException
     * @author 魏荣杰
     * @date 2019/5/23 21:08
     * @since v1.0
     */
    public <T> void reductionEsInUpdate(T entity, Long id, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        try {
            //id为空没必要往下进行
            if (id == null || id == 0) {
                return ;
            }
            switch (indexTypeEnum) {
                case IDX_FORM_DATA:
                    break;
                case IDX_SAAS_CUSTOMER:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    IndexTypeEnum customerSubIndex = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());
                    reductionSubEsData(id, corpid, customerSubIndex);
                    break;
                case IDX_SAAS_CUSTOMER_TEAM:
                    break;
                case IDX_SAAS_CONTRACT:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_SUB);
                    break;
                case IDX_SAAS_CONTRACT_PRODUCT:
                    break;
                case IDX_SAAS_CONTRACT_TEAM:
                    break;
                case IDX_SAAS_OUTSTOCK:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_OUTSTOCK_SUB);
                    break;
                case IDX_SAAS_CONTACT:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_CONTACT_SUB);
                    break;
                case IDX_SAAS_CONTACT_TEAM:
                    break;
                case IDX_SAAS_INVOICE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_INVOICE_SUB);
                    break;
                case IDX_SAAS_PURCHASE_INVOICE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_SUB);
                    break;
                case IDX_SAAS_INSTOCK:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_INSTOCK_SUB);
                    break;
                case IDX_SAAS_ASSEMBLE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_ASSEMBLE_SUB);
                    break;
                case IDX_SAAS_PAYMENT:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SUB);
                    //恢复应收款的团队信息表
                    reductionEsPaymentTeam(id, corpid);
                    break;
                case IDX_SAAS_PAYMENT_TEAM:
                    break;
                case IDX_SAAS_PAYMENT_SHEET:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
                    //恢复团队信息表
                    reductionEsPaymentSheetTeam(id, corpid);
                    break;
                case IDX_SAAS_PAYPLAN_SHEET:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_SUB);
                    //恢复团队信息表
                    reductionEsPaySheetTeam(id, corpid);
                    break;
                case IDX_SAAS_PRODUCTION_ORDER:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PRODUCTION_ORDER_SUB);
                    break;
                case IDX_SAAS_PRODUCTION_ORDER_TEAM:
                    break;
                case IDX_SAAS_PAY_PLAN:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PAY_PLAN_SUB);
                    //恢复付款计划的团队信息表
                    reductionEsPayPlanTeam(id, corpid);
                    break;
                case IDX_SAAS_PAY_PLAN_TEAM:
                    break;
                case IDX_SAAS_OTHER_INCOME:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_OTHER_INCOME_SUB);
                    break;
                case IDX_SAAS_OTHER_EXPENSE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_OTHER_EXPENSE_SUB);
                    break;
                case IDX_SAAS_FUND_TRANSFER:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
                    break;
                case IDX_SAAS_COMMUNICATE_PLAN:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_SUB);
                    break;
                case IDX_SAAS_OPPORTUNITY:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_SUB);
                    break;
                case IDX_SAAS_OPPORTUNITY_TEAM:
                    break;
                case IDX_SAAS_BOM:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_BOM_SUB);
                    break;
                case IDX_SAAS_BOM_TEAM:
                    break;
                case IDX_SAAS_CUSTOMER_COMMUNICATE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_CUSTOMER_COMMUNICATE_SUB);
                    break;
                case IDX_SAAS_SUPPLIER_COMMUNICATE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_SUPPLIER_COMMUNICATE_SUB);
                    break;
                case IDX_SAAS_WORK_REPORT:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_WORK_REPORT_SUB);
                    break;
                case IDX_SAAS_PURCHASE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PURCHASE_SUB);
                    break;
                case IDX_SAAS_PURCHASE_TEAM:
                    break;
                case IDX_SAAS_RETURNED_PURCHASE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_RETURNED_PURCHASE_SUB);
                    break;
                case IDX_SAAS_SUPPLIER_CONTACT:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_SUPPLIER_CONTACT_SUB);
                    break;
                case IDX_SAAS_SUPPLIER_CONTACT_TEAM:
                    break;
                case IDX_SAAS_SUPPLIER:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_SUPPLIER_SUB);
                    break;
                case IDX_SAAS_SUPPLIER_TEAM:
                    break;
                case IDX_SAAS_PRODUCT:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PRODUCT_SUB);
                    break;
                case IDX_SAAS_PRODUCT_TEAM:
                case IDX_SERVICE_PROJECT_TEAM:
                    break;
                case IDX_SAAS_INVENTORY:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_INVENTORY_SUB);
                    break;
                case IDX_SAAS_TRANSFER:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_TRANSFER_SUB);
                    break;
                case IDX_SAAS_WAREHOUSE:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_WAREHOUSE_SUB);
                    break;
                case IDX_SAAS_REFUND:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_REFUND_SUB);
                    // 还原退货退款团队
                    reductionEsRefundTeam(id, corpid);
                    break;
                case IDX_SAAS_REFUND_TEAM:
                    // 还原退货退款团队
                    reductionEsRefundTeam(id, corpid);
                    break;
                case IDX_SAAS_WORK_ORDER:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_WORK_ORDER_SUB);
                    //还原工单团队
                    reductionEsWorkOrderTeam(id, corpid);
                    break;
                case IDX_SAAS_WORK_ORDER_TEAM:
                    //还原工单团队
                    reductionEsWorkOrderTeam(id, corpid);
                    break;
                case IDX_SAAS_WORK_ORDER_CC:
                    //这个没有
                    break;
                case IDX_CALL_CENTER_CALL_LOG:
                    break;
                case IDX_SAAS_QUOTATION:
                    //恢复主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_QUOTATION_SUB);
                    break;
                case IDX_SAAS_PAYMENT_TASK:
                    //恢复回款计划主表数据
                    reductionEsData(entity, id, corpid, indexTypeEnum);
                    //恢复回款计划执行者数据
                    reductionEsPaymentTaskTeam(id, corpid);
                    // 恢复子表数据
                    reductionSubEsData(id, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
                    break;
                case UNKNOWN:
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            LOG.error("ProEsHelper.reductionEsInUpdate 出错, id=" + id + "  corpid=" + corpid + " IndexTypeEnum=" + indexTypeEnum.toString(), e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }


    }

    /**
     * Description:回滚子表单es
     * @param id
     * @param corpid
     * @param indexTypeEnum
     * @return void
     * @throws
     * @author 魏荣杰
     * @date 2020/10/10 13:35
     * @since
     */
    private void reductionSubEsData(Long id, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        List<PaasFormSubDataEntity> search = search(indexTypeEnum, corpid, StringConstant.DATA_ID, id, PaasFormSubDataEntity.class);
        Map<Long, PaasFormSubDataEntity> esMap = new HashMap<>();
        for (PaasFormSubDataEntity paasFormDataEntity : search) {
            esMap.put(paasFormDataEntity.getId(), paasFormDataEntity);
        }
        List<UpdateDataEntity> updateList = new ArrayList<>();
        List<PaasFormSubDataEntity> deleteList = new ArrayList<>();
        List<? extends PaasFormSubDataEntity> dbPaasFormSubDataEntityList;

        switch (indexTypeEnum) {
            case IDX_SUB_FORM_DATA:
                dbPaasFormSubDataEntityList = paasFormPaasSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    paasFormPaasSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SUB_FORM_DATA);
                }
                break;
            case IDX_SAAS_CUSTOMER_SUB:
                dbPaasFormSubDataEntityList = customerSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    customerSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    IndexTypeEnum customerTeamIndex = indexTypeModel.getCustomerTeamIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

                    physicalDeleteSubBatch(corpid, deleteList, customerTeamIndex);
                }
                break;
            case IDX_SAAS_CONTRACT_SUB:
                dbPaasFormSubDataEntityList = contractSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    contractSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_CONTRACT_SUB);
                }
                break;
            case IDX_SAAS_QUOTATION_SUB:
                dbPaasFormSubDataEntityList = quotationSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    quotationSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_QUOTATION_SUB);
                }
                break;
            case IDX_SAAS_OUTSTOCK_SUB:
                dbPaasFormSubDataEntityList = outstockSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    outstockSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_OUTSTOCK_SUB);
                }
                break;
            case IDX_SAAS_CONTACT_SUB:
                dbPaasFormSubDataEntityList = contactSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    contactSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_CONTACT_SUB);
                }
                break;
            case IDX_SAAS_INVOICE_SUB:
                dbPaasFormSubDataEntityList = invoiceSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    invoiceSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_INVOICE_SUB);
                }
                break;
            case IDX_SAAS_PURCHASE_INVOICE_SUB:
                dbPaasFormSubDataEntityList = purchaseInvoiceSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    purchaseInvoiceSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_SUB);
                }
                break;
            case IDX_SAAS_INSTOCK_SUB:
                dbPaasFormSubDataEntityList = instockSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    instockSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_INSTOCK_SUB);
                }
                break;
            case IDX_SAAS_ASSEMBLE_SUB:
                dbPaasFormSubDataEntityList = assembleSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    assembleSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_ASSEMBLE_SUB);
                }
                break;
            case IDX_SAAS_PAYMENT_SUB:
                dbPaasFormSubDataEntityList = paymentSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    paymentSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PAYMENT_SUB);
                }
                break;
            case IDX_SAAS_PAYMENT_SHEET_SUB:
                dbPaasFormSubDataEntityList = paymentSheetSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    paymentSheetSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
                }
                break;
            case IDX_SAAS_PAYMENT_TASK_SUB:
                dbPaasFormSubDataEntityList = paymentTaskSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    paymentTaskSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
                }
                break;
            case IDX_SAAS_PAYPLAN_SHEET_SUB:
                dbPaasFormSubDataEntityList = payPlanSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    payPlanSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_SUB);
                }
                break;
            case IDX_SAAS_PRODUCTION_ORDER_SUB:
                dbPaasFormSubDataEntityList = productionOrderSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    productionOrderSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PRODUCTION_ORDER_SUB);
                }
                break;
            case IDX_SAAS_PAY_PLAN_SUB:
                dbPaasFormSubDataEntityList = payPlanSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    payPlanSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PAY_PLAN_SUB);
                }
                break;
            case IDX_SAAS_OTHER_INCOME_SUB:
                dbPaasFormSubDataEntityList = otherIncomeSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    otherIncomeSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_OTHER_INCOME_SUB);
                }
                break;
            case IDX_SAAS_OTHER_EXPENSE_SUB:
                dbPaasFormSubDataEntityList = otherExpenseSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    otherExpenseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_OTHER_EXPENSE_SUB);
                }
                break;
            case IDX_SAAS_FUND_TRANSFER_SUB:
                dbPaasFormSubDataEntityList = fundTransferSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    fundTransferSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
                }
                break;
            case IDX_SAAS_COMMUNICATE_PLAN_SUB:
                dbPaasFormSubDataEntityList = communicatePlanSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    communicatePlanSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_SUB);
                }
                break;
            case IDX_SAAS_OPPORTUNITY_SUB:
                dbPaasFormSubDataEntityList = opportunitySubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    opportunitySubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_SUB);
                }
                break;
            case IDX_SAAS_BOM_SUB:
                dbPaasFormSubDataEntityList = bomBillSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    bomBillSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_BOM_SUB);
                }
                break;
            case IDX_SAAS_CUSTOMER_COMMUNICATE_SUB:
                dbPaasFormSubDataEntityList = customerCommunicateSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    customerCommunicateSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_CUSTOMER_COMMUNICATE_SUB);
                }
                break;
            case IDX_SAAS_SUPPLIER_COMMUNICATE_SUB:
                dbPaasFormSubDataEntityList = supplierCommunicateSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    supplierCommunicateSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_SUPPLIER_COMMUNICATE_SUB);
                }
                break;
            case IDX_SAAS_WORK_REPORT_SUB:
                dbPaasFormSubDataEntityList = workReportSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    workReportSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_WORK_REPORT_SUB);
                }
                break;
            case IDX_SAAS_PURCHASE_SUB:
                dbPaasFormSubDataEntityList = purchaseSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    purchaseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PURCHASE_SUB);
                }
                break;
            case IDX_SAAS_RETURNED_PURCHASE_SUB:
                dbPaasFormSubDataEntityList = returnedPurchaseSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    returnedPurchaseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_RETURNED_PURCHASE_SUB);
                }
                break;
            case IDX_SAAS_SUPPLIER_CONTACT_SUB:
                dbPaasFormSubDataEntityList = supplierContactSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    supplierContactSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_SUPPLIER_CONTACT_SUB);
                }
                break;
            case IDX_SAAS_SUPPLIER_SUB:
                dbPaasFormSubDataEntityList = supplierSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    supplierSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_SUPPLIER_SUB);
                }
                break;
            case IDX_SAAS_PRODUCT_SUB:
                dbPaasFormSubDataEntityList = productSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    productSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_PRODUCT_SUB);
                }
                break;
            case IDX_SAAS_INVENTORY_SUB:
                dbPaasFormSubDataEntityList = inventorySubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    inventorySubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_INVENTORY_SUB);
                }
                break;
            case IDX_SAAS_TRANSFER_SUB:
                dbPaasFormSubDataEntityList = transferSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    transferSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_TRANSFER_SUB);
                }
                break;
            case IDX_SAAS_WAREHOUSE_SUB:
                dbPaasFormSubDataEntityList = warehouseSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    warehouseSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_WAREHOUSE_SUB);
                }
                break;
            case IDX_SAAS_REFUND_SUB:
                dbPaasFormSubDataEntityList = refundSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    refundSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_REFUND_SUB);
                }
                break;
            case IDX_SAAS_WORK_ORDER_SUB:
                dbPaasFormSubDataEntityList = workOrderSubDataModel.getByDataId(id, corpid);
                for (PaasFormSubDataEntity paasFormSubDataEntity : dbPaasFormSubDataEntityList) {
                    if (esMap.containsKey(paasFormSubDataEntity.getId())) {
                        UpdateDataEntity updateData = ExplainUtil.getUpdateData(paasFormSubDataEntity.getId(), paasFormSubDataEntity.getData(), corpid);
                        updateList.add(updateData);
                    } else {
                        deleteList.add(esMap.get(paasFormSubDataEntity.getId()));
                    }
                }
                if (!updateList.isEmpty()) {
                    workOrderSubDataModel.updateBatch(updateList, corpid);
                }
                if (!deleteList.isEmpty()) {
                    physicalDeleteSubBatch(corpid, deleteList, IndexTypeEnum.IDX_SAAS_WORK_ORDER_SUB);
                }
                break;
            default:
                break;
        }

    }


    /**
     * 恢复应收款的团队信息表
     *
     * @param id
     * @param corpid
     */
    private void reductionEsPaymentTeam(Long id, String corpid) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("dataId", id);
        map.put("del", 0);
        List<PaymentUserEntity> dbList = paymentUserModel.findEntitys(map);
        List<InsertDTO> updateTeamList = new ArrayList<>();
        /*
        for (PaymentUserEntity paymentUserEntity : paymentUserEntities) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setParent(corpid + "_" + paymentUserEntity.getDataId());
            Map<String, Object> param = BeanUtil.convertBean2Map(paymentUserEntity, false, true);
            insertDTO.setSource(param);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM);
            updatePaymentUser.add(insertDTO);
        }*/
        Set<Long> userIds = new HashSet<>();
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM, dbList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            userIds.add(paasFormDataEntity.getId());
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM);
            updateTeamList.add(insertDTO);
        }
        //还原原数据
        paasEsModel.updateBatch(updateTeamList);

        List<PaasFormDataEntity> esTeamList = search(IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM, corpid, PaymentTeamEnum.getEsAttr(PaymentTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
        Set<Long> esUserIds = new HashSet<>();
        for (PaasFormDataEntity team : esTeamList) {
            esUserIds.add(team.getId());
        }
        //获取需要删除的id
        esUserIds.removeAll(userIds);
        //删除团队数据
        paasEsModel.physicalDeleteBatch(new ArrayList<>(esUserIds), corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM);
    }
    /**
     * 恢复应收款的团队信息表
     *
     * @param id
     * @param corpid
     */
    private void reductionEsPayPlanTeam(Long id, String corpid) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("dataId", id);
        map.put("del", 0);
        List<PayPlanUserEntity> dbList = payPlanUserModel.findEntitys(map);
        List<InsertDTO> updateTeamList = new ArrayList<>();
        Set<Long> userIds = new HashSet<>();
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_PAY_PLAN_TEAM, dbList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            userIds.add(paasFormDataEntity.getId());
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAY_PLAN_TEAM);
            updateTeamList.add(insertDTO);
        }
        //还原原数据
        paasEsModel.updateBatch(updateTeamList);

        List<PaasFormDataEntity> esTeamList = search(IndexTypeEnum.IDX_SAAS_PAY_PLAN_TEAM, corpid, PayPlanTeamEnum.getEsAttr(PayPlanTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
        Set<Long> esUserIds = new HashSet<>();
        for (PaasFormDataEntity team : esTeamList) {
            esUserIds.add(team.getId());
        }
        //获取需要删除的id
        esUserIds.removeAll(userIds);
        //删除团队数据
        paasEsModel.physicalDeleteBatch(new ArrayList<>(esUserIds), corpid, IndexTypeEnum.IDX_SAAS_PAY_PLAN_TEAM);
    }

    /**
     * 回款单团队更新
     *
     * @param id 回款单id
     * @param corpid 公司id
     */
    private void reductionEsPaymentSheetTeam(Long id, String corpid) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("dataId", id);
        map.put("del", 0);
        List<PaymentSheetUserEntity> dbList = paymentSheetUserModel.findEntitys(map);
        List<InsertDTO> updateTeamList = new ArrayList<>();
        /*
        for (PaymentSheetUserEntity entity : entitys) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> param = BeanUtil.convertBean2Map(entity, false, true);
            insertDTO.setSource(param);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_TEAM);
            updateUser.add(insertDTO);
        }*/
        Set<Long> userIds = new HashSet<>();
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_TEAM, dbList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            userIds.add(paasFormDataEntity.getId());
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_TEAM);
            updateTeamList.add(insertDTO);
        }
        //还原原数据
        paasEsModel.updateBatch(updateTeamList);

        List<PaasFormDataEntity> esTeamList = search(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_TEAM, corpid, PaymentSheetTeamEnum.getEsAttr(PaymentSheetTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
        Set<Long> esUserIds = new HashSet<>();
        for (PaasFormDataEntity team : esTeamList) {
            esUserIds.add(team.getId());
        }
        //获取需要删除的id
        esUserIds.removeAll(userIds);
        //删除团队数据
        paasEsModel.physicalDeleteBatch(new ArrayList<>(esUserIds), corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_TEAM);
    }

    /**
     * 付款单团队更新
     *
     * @param id 付款单id
     * @param corpid 公司id
     */
    private void reductionEsPaySheetTeam(Long id, String corpid) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("dataId", id);
        map.put("del", 0);
        List<PayPlanSheetUserEntity> dbList = payPlanSheetUserModel.findEntitys(map);
        List<InsertDTO> updateTeamList = new ArrayList<>();
        /*
        for (PayPlanSheetUserEntity entity : entitys) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> param = BeanUtil.convertBean2Map(entity, false, true);
            insertDTO.setSource(param);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_TEAM);
            updateUser.add(insertDTO);
        }*/
        Set<Long> userIds = new HashSet<>();
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_TEAM, dbList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            userIds.add(paasFormDataEntity.getId());
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_TEAM);
            updateTeamList.add(insertDTO);
        }
        //还原原数据
        paasEsModel.updateBatch(updateTeamList);

        List<PaasFormDataEntity> esTeamList = search(IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_TEAM, corpid, PayPlanSheetTeamEnum.getEsAttr(PayPlanSheetTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
        Set<Long> esUserIds = new HashSet<>();
        for (PaasFormDataEntity team : esTeamList) {
            esUserIds.add(team.getId());
        }
        //获取需要删除的id
        esUserIds.removeAll(userIds);
        //删除团队数据
        paasEsModel.physicalDeleteBatch(new ArrayList<>(esUserIds), corpid, IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_TEAM);
    }

    /**
     * 回款计划执行者更新
     *
     * @param id 回款计划id
     * @param corpid 公司id
     */
    private void reductionEsPaymentTaskTeam(Long id, String corpid) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("dataId", id);
        map.put("del", 0);
        List<PaymentTaskUserEntity> paymentTaskUserEntities = paymentTaskUserModel.findEntitys(map);
        List<InsertDTO> updatePaymentTaskUser = new ArrayList<>();
        for (PaymentTaskUserEntity paymentTaskUserEntity : paymentTaskUserEntities) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM.getType() + "_" + corpid + "_" + paymentTaskUserEntity.getId());
            insertDTO.setParent(corpid + "_" + paymentTaskUserEntity.getDataId());
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(PaymentTaskTeamEnum.DATA_ID.getDataAttr(), paymentTaskUserEntity.getDataId());
            object.put(PaymentTaskTeamEnum.USER_ID.getDataAttr(), paymentTaskUserEntity.getUserId());
            object.put(PaymentTaskTeamEnum.USER_NAME.getDataAttr(), paymentTaskUserEntity.getUserName());
            object.put(PaymentTaskTeamEnum.USER_AVATAR.getDataAttr(), paymentTaskUserEntity.getUserAvatar());
            param.put(StringConstant.JSON_DATA, object);
            insertDTO.setSource(param);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM);
            updatePaymentTaskUser.add(insertDTO);
        }
        //还原原数据
        paasEsModel.updateBatch(updatePaymentTaskUser);
    }
    /**
     * 更新回滚
     * @param entity
     * @param id
     * @param corpid
     * @param businessType
     * @param saasMark
     * @param <T>
     * @throws XbbException
     */
    public <T extends PaasFormDataEntity> void reductionEsInUpdate(T entity, Long id, String corpid, Integer businessType , Integer saasMark) throws XbbException {
        PaasFormDataEsMappingDTO paasFormDataEsMappingDTO = new PaasFormDataEsMappingDTO();
        paasFormDataEsMappingDTO.setSaasMark(saasMark);
        SheetTypeEnum sheetTypeEnum = SheetTypeEnum.getByBusinessType(businessType);
        boolean sheetFlag = SheetTypeEnum.validateSheet(sheetTypeEnum);
        if (sheetFlag) {
            paasFormDataEsMappingDTO.setBusinessType(sheetTypeEnum.getXbbRefTypeEnum().getCode());
        } else {
            paasFormDataEsMappingDTO.setBusinessType(businessType);
        }
        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(corpid, paasFormDataEsMappingDTO.getBusinessType(), saasMark);
        IndexTypeEnum subByIndexEnum = IndexTypeEnum.getSubByIndexEnum(indexTypeEnum);
        paasFormSubDataModel.transferSubForm(entity, subByIndexEnum);
        paasEsModel.update(entity,indexTypeEnum,corpid,id,null);
        //TODO 从表的处理
    }



    /**
     * Description: 还原工单团队
     * @param id
     * @param corpid
     * @return void
     * @throws
     * @author 魏荣杰
     * @date 2019/5/24 10:54
     * @since v1.0
     */
    private void reductionEsWorkOrderTeam(Long id, String corpid) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("workOrderId", id);
        map.put("del", 0);
        List<WorkOrderFlowUserEntity> dBWorkOrderFlowUserList = workOrderFlowUserModel.findEntitys(map);
        Set<Long> workOrderFlowUserIds = new HashSet<>();
        List<InsertDTO> updateWokOrderFlowUser = new ArrayList<>();
//        for (WorkOrderFlowUserEntity workOrderFlowUserEntity : dBWorkOrderFlowUserList) {
//            workOrderFlowUserIds.add(workOrderFlowUserEntity.getId());
//            InsertDTO insertDTO = new InsertDTO();
//            insertDTO.setEsId(corpid + "_" + workOrderFlowUserEntity.getId());
//            insertDTO.setParent(corpid + "_" + workOrderFlowUserEntity.getDataId());
//            Map<String, Object> param = BeanUtil.convertBean2Map(workOrderFlowUserEntity, false, true);
//            insertDTO.setSource(param);
//            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
//            updateWokOrderFlowUser.add(insertDTO);
//        }
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, dBWorkOrderFlowUserList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            workOrderFlowUserIds.add(paasFormDataEntity.getId());
            updateWokOrderFlowUser.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM));
        }

        //还原原数据
        paasEsModel.updateBatch(updateWokOrderFlowUser);

        List<PaasFormDataEntity> workOrderFlowUserEntities = search(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, corpid, WorkOrderTeamEnum.getEsAttr(WorkOrderTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
        Set<Long> esWorkOrderUserIds = new HashSet<>();
        for (PaasFormDataEntity workOrderFlowUserEntity : workOrderFlowUserEntities) {
            esWorkOrderUserIds.add(workOrderFlowUserEntity.getId());
        }
        //获取需要删除的id
        esWorkOrderUserIds.removeAll(workOrderFlowUserIds);
        //删除团队数据
        paasEsModel.physicalDeleteBatch(new ArrayList<>(esWorkOrderUserIds), corpid, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
    }

    /**
     * Description: 还原退货退款团队
     * @param id
     * @param corpid
     * @return void
     * @throws
     * @author youli.chen
     * @date 2019/5/24 10:54
     * @since v1.0
     */
    private void reductionEsRefundTeam(Long id, String corpid) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("refundId", id);
        map.put("del", 0);
        List<RefundUserEntity> dBRefundUserList = refundUserModel.findEntitys(map);
        Set<Long> refundUserUserIds = new HashSet<>();
        List<InsertDTO> updateRefundUser = new ArrayList<>();
//        for (RefundUserEntity refundUserEntity : dBRefundUserList) {
//            refundUserUserIds.add(refundUserEntity.getId());
//            InsertDTO insertDTO = new InsertDTO();
//            insertDTO.setEsId(corpid + "_" + refundUserEntity.getId());
//            insertDTO.setParent(corpid + "_" + refundUserEntity.getDataId());
//            Map<String, Object> param = BeanUtil.convertBean2Map(refundUserEntity, false, true);
//            insertDTO.setSource(param);
//            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_REFUND_TEAM);
//            updateRefundUser.add(insertDTO);
//        }
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_REFUND_TEAM, dBRefundUserList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            refundUserUserIds.add(paasFormDataEntity.getId());
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_REFUND_TEAM);
            updateRefundUser.add(insertDTO);
        }


        //还原原数据
        paasEsModel.updateBatch(updateRefundUser);

        List<PaasFormDataEntity> refundUserEntities = search(IndexTypeEnum.IDX_SAAS_REFUND_TEAM, corpid, RefundTeamEnum.getEsAttr(RefundTeamEnum.DATA_ID), id, PaasFormDataEntity.class);
        Set<Long> esRefundUserIds = new HashSet<>();
        for (PaasFormDataEntity refundUserEntity : refundUserEntities) {
            esRefundUserIds.add(refundUserEntity.getId());
        }
        //获取需要删除的id
        esRefundUserIds.removeAll(refundUserUserIds);
        //删除团队数据
        paasEsModel.physicalDeleteBatch(new ArrayList<>(esRefundUserIds), corpid, IndexTypeEnum.IDX_SAAS_REFUND_TEAM);
    }

    /**
     * Description: 恢复主表数据, 通用
     * @param entity
     * @param id
     * @param corpid
     * @param indexTypeEnum
     * @return void
     * @throws
     * @author 魏荣杰
     * @date 2019/5/24 10:52
     * @since v1.0
     */
    private <T> void reductionEsData(T entity, Long id, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException,IOException {
        RestHighLevelClient client = xbbElasticsearchRestTemplate.getRestHighLevelClient(indexTypeEnum.getIndex());
        UpdateRequest updateRequest = new UpdateRequest();

        IndexTypeEnum subByIndexEnum = IndexTypeEnum.getSubByIndexEnum(indexTypeEnum);
        paasFormSubDataModel.transferSubForm((PaasFormDataEntity) entity, subByIndexEnum);

        Map<String, Object> map = BeanUtil.convertBean2Map(entity, true, true);
        updateRequest.id(corpid + "_" + id)
                .index(indexTypeEnum.getIndex())
                .doc(new JSONObject(map).toJSONString(), Requests.INDEX_CONTENT_TYPE)
                .setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL)
                .retryOnConflict(5);
        client.update(updateRequest, RequestOptions.DEFAULT);
    }

    /**
     * Description:
     * @param indexTypeEnum 需要查询父/子的index
     * @param corpid 公司id
     * @param idStr 主体id在es中存储的key值
     * @param id 主体id
     * @param clazz 返回对象
     * @author 魏荣杰
     * @date 2019/5/23 10:58
     * @since v1.0
     */
    private List search(IndexTypeEnum indexTypeEnum, String corpid, String idStr, Long id, Class clazz) throws XbbException {
        List list = new ArrayList();
        try {
            //id为空没必要往下进行
            if (id == null || id == 0) {
                return list;
            }
            //查询步长默认设置为10000
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = boolQuery();
            boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
            boolQueryBuilder.filter(termQuery(idStr, id));
            boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,indexTypeEnum.getType()));
            sourceBuilder.query(boolQueryBuilder);
            SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
            EsUtil.setPage(sourceBuilder, 0, SizeConstant.PAGE_SIZE);
            searchRequest.source(sourceBuilder);
            list = xbbElasticsearchRestTemplate.findScrollList(clazz, searchRequest, SizeConstant.SCROLL_TIME_IN_MILLIS);
        } catch (Exception e) {
            LOG.error("ProEsHelper.search 出错， id=" + id + "  corpid=" + corpid + "  IndexTypeEnum=" + indexTypeEnum.toString(), e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return list;
    }

    /**
     * 物理删除新增退货退款失败的数据
     * @param id 退货退款ID
     * @param corpid 公司ID
     * @param redContractIdList 红冲合同ID集合
     * @param redContractId 红冲合同ID
     * @param oldContractId 退货退款关联原合同ID
     * @throws XbbException
     */
    private void physicalDeleteRefundInsert(Long id, String corpid, List<Long> redContractIdList, Long redContractId, Long oldContractId) throws XbbException {
        if(redContractIdList.size() > 0){
            // 删除红冲合同
            paasEsModel.physicalDeleteBatch(redContractIdList, corpid, IndexTypeEnum.IDX_SAAS_CONTRACT);
            // 查看原合同是否还有关联退货退款，如果没有，将合同状态置回，避免被污染
            BoolQueryBuilder boolQueryBuilder = boolQuery();
            boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
            boolQueryBuilder.filter(termQuery(RefundEnum.getEsAttr4Keyword(RefundEnum.CONTRACT_ID),oldContractId));
            boolQueryBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
            // 限制返回值
            List<String> fieldList = Arrays.asList(FieldTypeEnum.DATAID.getAlias(), FieldTypeEnum.SERIALNO.getAlias());
            List<PaasFormDataEntityExt> refundExtList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_REFUND,boolQueryBuilder,PaasFormDataEntityExt.class,fieldList);
            if(Objects.equals(refundExtList.size(), 0)){
                BoolQueryBuilder contractBuilder = boolQuery();
                contractBuilder.filter(termQuery("corpid.keyword", corpid));
                contractBuilder.filter(termsQuery(FieldTypeEnum.DATAID.getAlias(), Arrays.asList(oldContractId)));
                contractBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
                // 限制返回值
//                List<String> contractFieldList = Arrays.asList(FieldTypeEnum.DATAID.getAlias(), FieldTypeEnum.SERIALNO.getAlias(), StringConstant.JSON_DATA);
                List<ContractEntityExt> contractExtList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_CONTRACT,contractBuilder,ContractEntityExt.class,null);
                for(ContractEntityExt paasFormDataEntityExt : contractExtList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    data.put(ContractEnum.HAS_RED.getAttr(), BasicConstant.ZERO);
                    data.put(ContractEnum.IS_RED.getAttr(), BasicConstant.ONE);
                    paasFormDataEntityExt.setData(data);
                    contractModel.update(paasFormDataEntityExt);
                }
            }
        }
        if(redContractId != null){
            // 删除红冲合同销售团队
            List<PaasFormDataEntity> contractTeamEntities = search(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM, corpid, ContactTeamEnum.getAttrConnectData(ContactTeamEnum.DATA_ID), redContractId, PaasFormDataEntity.class);
            List<ContractUserEntity> userTeamEntities = (List<ContractUserEntity>) transferSubFormHelper.transferFormDataToUserTeamList(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM, contractTeamEntities);

            List<DeleteDTO> deleteContractUserList = new ArrayList<>();
            for (ContractUserEntity entity : userTeamEntities) {
                DeleteDTO deleteDTO = new DeleteDTO();
                deleteDTO.setEsId(corpid + "_" + entity.getId());
                deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);
                deleteDTO.setParent(corpid + "_" + entity.getDataId());
                deleteContractUserList.add(deleteDTO);
            }
            paasEsModel.physicalDeleteBatchChild(deleteContractUserList, corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM);

            // 删除红冲合同业绩分配
            List<PaasFormDataEntity> contractPerformanceEntities = search(IndexTypeEnum.IDX_SAAS_CONTRACT_PERFORMANCE, corpid, ContractPerformanceSubFormEnum.getEsAttr(ContractPerformanceSubFormEnum.CONTRACT_ID), redContractId, PaasFormDataEntity.class);
            List<ContractPerformanceEntity> contractPerformanceEntityList = transferSubFormHelper.transferFormDataToContractPerformanceList(contractPerformanceEntities);
            List<DeleteDTO> deletePerformanceList = new ArrayList<>();
            for (ContractPerformanceEntity entity : contractPerformanceEntityList) {
                DeleteDTO deleteDTO = new DeleteDTO();
                deleteDTO.setEsId(corpid + "_" + entity.getId());
                deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_PERFORMANCE);
                deleteDTO.setParent(corpid + "_" + entity.getContractId());
                deleteContractUserList.add(deleteDTO);
            }
            paasEsModel.physicalDeleteBatchChild(deletePerformanceList, corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_PERFORMANCE);

            // 删除红冲合同产品
            List<PaasFormDataEntity> contractProductEntities = search(IndexTypeEnum.IDX_SAAS_CONTRACT_PRODUCT, corpid, ContractProductSubFormEnum.getEsAttr(ContractProductSubFormEnum.CONTRACT_ID), redContractId, PaasFormDataEntity.class);
            List<ContractProductEntity> contractProductEntityList = transferSubFormHelper.transferFormDataToContractProductList(contractProductEntities);
            List<DeleteDTO> deleteContractProductList = new ArrayList<>();
            for (ContractProductEntity entity : contractProductEntityList) {
                DeleteDTO deleteDTO = new DeleteDTO();
                deleteDTO.setEsId(corpid + "_" + entity.getId());
                deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTRACT_PRODUCT);
                deleteDTO.setParent(corpid + "_" + entity.getContractId());
                deleteContractProductList.add(deleteDTO);
            }
            paasEsModel.physicalDeleteBatchChild(deleteContractProductList, corpid, IndexTypeEnum.IDX_SAAS_CONTRACT_PRODUCT);

            // 删除红冲发票
            refundInvoiceModel.deleteByContractId(redContractId, corpid);

            // 删除红冲回款单
            BoolQueryBuilder paymentSheetBuilder = boolQuery();
            paymentSheetBuilder.filter(termQuery("corpid.keyword", corpid));
            paymentSheetBuilder.filter(termQuery(PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.CONTRACT), redContractId));
            paymentSheetBuilder.filter(termQuery(PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.IS_RED), BasicConstant.ZERO));
            paymentSheetBuilder.filter(termQuery(PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.REF_TYPE), PaymentLinkTypeEnum.REFUND.getCode()));
            paymentSheetBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
            // 限制返回值
            List<String> paymentSheetFieldList = Arrays.asList(FieldTypeEnum.DATAID.getAlias(), FieldTypeEnum.SERIALNO.getAlias());
            List<PaasFormDataEntityExt> paymentSheetExtList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET,paymentSheetBuilder,PaasFormDataEntityExt.class,paymentSheetFieldList);
            List<Long> paymentSheetIds = new ArrayList<>();
            for (PaasFormDataEntityExt entity : paymentSheetExtList) {
                paymentSheetIds.add(entity.getDataId());
            }
            paasEsModel.physicalDeleteBatch(paymentSheetIds, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        }

        // 删除红冲应收款
        BoolQueryBuilder paymentBuilder = boolQuery();
        paymentBuilder.filter(termQuery("corpid.keyword", corpid));
        paymentBuilder.filter(termQuery(PaymentEnum.getEsAttr4Keyword(PaymentEnum.CONTRACT), id));
        paymentBuilder.filter(termQuery(PaymentEnum.getEsAttr4Keyword(PaymentEnum.IS_RED), BasicConstant.ONE));
        paymentBuilder.filter(termQuery(PaymentEnum.getEsAttr4Keyword(PaymentEnum.REF_TYPE), PaymentLinkTypeEnum.REFUND.getCode()));
        paymentBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
        // 限制返回值
        List<String> paymentFieldList = Arrays.asList(FieldTypeEnum.DATAID.getAlias(), FieldTypeEnum.SERIALNO.getAlias());
        List<PaasFormDataEntityExt> paymentExtList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PAYMENT,paymentBuilder,PaasFormDataEntityExt.class,paymentFieldList);
        List<Long> paymentIds = new ArrayList<>();
        paymentExtList.forEach(item -> paymentIds.add(item.getDataId()));
        paasEsModel.physicalDeleteBatch(paymentIds, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT);

        // 删除红冲应收款关联团队
        if(paymentIds.size() > 0){
            for(Long paymentId : paymentIds){
                List<PaasFormDataEntity> paymentUserEntities = search(IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM, corpid, PaymentTeamEnum.getEsAttr(PaymentTeamEnum.DATA_ID), paymentId, PaasFormDataEntity.class);
                List<PaymentUserEntity> userTeamEntities = (List<PaymentUserEntity>) transferSubFormHelper.transferFormDataToUserTeamList(IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM, paymentUserEntities);
                List<Long> paymentUserIds = new ArrayList<>();
                for (PaymentUserEntity entity : userTeamEntities) {
                    paymentUserIds.add(entity.getId());
                }
                paasEsModel.physicalDeleteBatch(paymentUserIds, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM);
            }
        }
    }

    /**
     * 处理新增加发票失败后的其他表的数据
     * @param invoiceEntityExtList 发票集合
     * @param corpid 公司ID
     * @throws XbbException
     */
    private void invoiceInsertErrorAfterHandle(List<PaasFormDataEntity> invoiceEntityExtList,String corpid,Integer bussinessType) throws XbbException,IOException {
        if(Objects.equals(bussinessType,XbbRefTypeEnum.INVOICE.getCode()) && CollectionsUtil.isNotEmpty(invoiceEntityExtList)) {
            List<ContractEntityExt> contractEntityExtList = null;
            List<PaymentEntityExt> paymentEntityExtList = null;
            List<PaymentSheetEntityExt> paymentSheetEntityExtList = null;
            Set<String> uuidSet = new HashSet<>();
            for (PaasFormDataEntity invoiceEntityExt : invoiceEntityExtList) {
                JSONObject data = invoiceEntityExt.getData();
                JSONArray contractArray = data.getJSONArray(InvoiceEnum.CONTRACT_ID.getAttr());
                JSONArray paymentArray = data.getJSONArray(InvoiceEnum.PAYMENT_ID.getAttr());
                JSONArray paymentSheetArray = data.getJSONArray(InvoiceEnum.PAYMENT_SHEET_ID.getAttr());
                List<Long> contractIdList = null;
                List<Long> paymentIdList = null;
                List<Long> paymentSheetIdList= null;
                if (CollectionsUtil.isNotEmpty(contractArray)) {
                    contractIdList = JSONArray.parseArray(contractArray.toJSONString(), Long.class);
                    contractEntityExtList = contractModel.findEntitysByIdIn(corpid,BasicConstant.ZERO,contractIdList);
                }
                if(CollectionsUtil.isNotEmpty(paymentArray)){
                    paymentIdList = JSONArray.parseArray(paymentArray.toJSONString(),Long.class);
                    paymentEntityExtList = paymentModel.getPaymentByIdIn(corpid,paymentIdList);
                }
                if(CollectionsUtil.isNotEmpty(paymentSheetArray)){
                    paymentSheetIdList = JSONArray.parseArray(paymentSheetArray.toJSONString(),Long.class);
                    paymentSheetEntityExtList = paymentSheetModel.getPaymentSheetByIdIn(corpid,paymentSheetIdList);
                }
            }
            if (CollectionsUtil.isNotEmpty(contractEntityExtList)) {
                for(ContractEntityExt contractEntityExt : contractEntityExtList){
                    reductionEsData(contractEntityExt,contractEntityExt.getId(),corpid,IndexTypeEnum.IDX_SAAS_CONTRACT);
                }
            }
            if(CollectionsUtil.isNotEmpty(paymentEntityExtList)){
                for(PaymentEntityExt paymentEntityExt : paymentEntityExtList){
                    reductionEsData(paymentEntityExt,paymentEntityExt.getId(),corpid,IndexTypeEnum.IDX_SAAS_PAYMENT);
                }
            }
            if(CollectionsUtil.isNotEmpty(paymentSheetEntityExtList)){
                for(PaymentSheetEntityExt paymentSheetEntityExt : paymentSheetEntityExtList){
                    uuidSet.add(paymentSheetEntityExt.getUuid());
                }
                List<PaymentSheetEntityExt> allPaymentSheet = paymentSheetModel.getParentAndSubPaymentSheet(corpid,new ArrayList<>(uuidSet));
                if(CollectionsUtil.isNotEmpty(allPaymentSheet)) {
                    for (PaymentSheetEntityExt paymentSheetEntityExt : allPaymentSheet) {
                        JSONObject data = paymentSheetEntityExt.getData();
                        if(!data.containsKey(PaymentSheetEnum.INVOICE_AMOUNT.getAttr())){
                            //兼容没有开票的情况
                            data.put(PaymentSheetEnum.INVOICE_AMOUNT.getAttr(),BasicConstant.ZERO_DOUBLE);
                        }
                        reductionEsData(paymentSheetEntityExt, paymentSheetEntityExt.getId(), corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
                    }
                }
            }
        }
    }

    /**
     * 处理新增加进项发票失败后的其他表的数据
     * @param purchaseInvoiceList 发票集合
     * @param corpid 公司ID
     * @throws XbbException
     */
    private void purchaseInvoiceInsertErrorAfterHandle(List<PaasFormDataEntity> purchaseInvoiceList,String corpid,Integer bussinessType) throws XbbException,IOException {
        if(Objects.equals(bussinessType,XbbRefTypeEnum.PURCHASE_INVOICE.getCode()) && CollectionsUtil.isNotEmpty(purchaseInvoiceList)) {
            List<PurchaseEntityExt> purchaseEntityExtList = null;
            List<PayPlanEntityExt> payPlanEntityExtList = null;
            List<PayPlanSheetEntityExt> paySheetEntityExtList = null;
            Set<String> uuidSet = new HashSet<>();
            for (PaasFormDataEntity purchaseInvoiceEntityExt : purchaseInvoiceList) {
                JSONObject data = purchaseInvoiceEntityExt.getData();
                JSONArray purchaseArray = data.getJSONArray(PurchaseInvoiceEnum.PURCHASE_ID.getAttr());
                JSONArray payPlanArray = data.getJSONArray(PurchaseInvoiceEnum.PAY_PLAN_ID.getAttr());
                JSONArray paySheetArray = data.getJSONArray(PurchaseInvoiceEnum.PAY_SHEET_ID.getAttr());
                List<Long> purchaseIdList = null;
                List<Long> payPlanIdList = null;
                List<Long> paySheetIdList= null;
                if (CollectionsUtil.isNotEmpty(purchaseArray)) {
                    purchaseIdList = JSONArray.parseArray(purchaseArray.toJSONString(), Long.class);
                    purchaseEntityExtList = purchaseModel.getPurchaseListByIdIn(corpid,purchaseIdList);
                }
                if(CollectionsUtil.isNotEmpty(payPlanArray)){
                    payPlanIdList = JSONArray.parseArray(payPlanArray.toJSONString(),Long.class);
                    payPlanEntityExtList = payPlanModel.getPayPlanByIdIn(corpid,payPlanIdList);
                }
                if(CollectionsUtil.isNotEmpty(paySheetArray)){
                    paySheetIdList = JSONArray.parseArray(paySheetArray.toJSONString(),Long.class);
                    paySheetEntityExtList = payPlanSheetModel.getPaySheetByIdIn(corpid,paySheetIdList);
                }
            }
            if (CollectionsUtil.isNotEmpty(purchaseEntityExtList)) {
                for(PurchaseEntityExt purchaseEntityExt : purchaseEntityExtList){
                    reductionEsData(purchaseEntityExt,purchaseEntityExt.getId(),corpid,IndexTypeEnum.IDX_SAAS_PURCHASE);
                }
            }
            if(CollectionsUtil.isNotEmpty(payPlanEntityExtList)){
                for(PayPlanEntityExt payPlanEntityExt : payPlanEntityExtList){
                    reductionEsData(payPlanEntityExt,payPlanEntityExt.getId(),corpid,IndexTypeEnum.IDX_SAAS_PAY_PLAN);
                }
            }
            if(CollectionsUtil.isNotEmpty(paySheetEntityExtList)){
                for(PayPlanSheetEntityExt paySheetEntityExt : paySheetEntityExtList){
                    uuidSet.add(paySheetEntityExt.getUuid());
                }
                List<PayPlanSheetEntityExt> allPaySheet = payPlanSheetModel.getParentAndSubPaySheet(corpid,new ArrayList<>(uuidSet));
                if(CollectionsUtil.isNotEmpty(allPaySheet)) {
                    for (PayPlanSheetEntityExt paySheetEntityExt : allPaySheet) {
                        JSONObject data = paySheetEntityExt.getData();
                        if(!data.containsKey(PaySheetEnum.INVOICE_AMOUNT.getAttr())){
                            //兼容没有开票的情况
                            data.put(PaySheetEnum.INVOICE_AMOUNT.getAttr(),BasicConstant.ZERO_DOUBLE);
                        }
                        reductionEsData(paySheetEntityExt, paySheetEntityExt.getId(), corpid, IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET);
                    }
                }
            }
        }
    }

    private void reRedErrorHandle(String corpid, Integer businessType, ReRollBackEntity reRollBackEntity) throws XbbException {
        List<Long> reRedSheetId = reRollBackEntity.getRedSheetId();
        List<Long> reRedPaymentId = reRollBackEntity.getRedPaymentId();
        List<Long> reRedInvoiceId = reRollBackEntity.getRedInvoiceId();
        if (XbbRefTypeEnum.REFUND.getCode() == businessType) {
            if (reRedSheetId != null && reRedSheetId.size() > 0) {
                paasEsModel.physicalDeleteBatch(reRedSheetId, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
            }
            if (reRedPaymentId != null && reRedPaymentId.size() > 0) {
                paasEsModel.physicalDeleteBatch(reRedSheetId, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT);
            }
            if (reRedInvoiceId != null && reRedInvoiceId.size() > 0) {
                paasEsModel.physicalDeleteBatch(reRedInvoiceId, corpid, IndexTypeEnum.IDX_SAAS_INVOICE);
            }
        } else if (XbbRefTypeEnum.RETURNED_PURCHASE.getCode() == businessType) {
            if (reRedSheetId != null && reRedSheetId.size() > 0) {
                paasEsModel.physicalDeleteBatch(reRedSheetId, corpid, IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET);
            }
            if (reRedInvoiceId != null && reRedInvoiceId.size() > 0) {
                paasEsModel.physicalDeleteBatch(reRedInvoiceId, corpid, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE);
            }
        }
    }
}
